<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-alert
          title="警告提示的文案"
          type="warning"
          show-icon>
      </el-alert>
      <el-row class="cat">
        <el-col>
          <span>选择商品分类:</span>
          <el-cascader
              v-model="selecatKeys"
              expand-trigger="hover"
              :options="catelist"
              :props="cateprops"
              @change="handleChange"></el-cascader>
        </el-col>
      </el-row>
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="动态参数" name="many">
          <el-button type="primary" size="mini" :disabled="isbtnable" @click="addDialog=true">添加参数</el-button>
       <el-table :data="manytableData" border stripe>
         <el-table-column type="expand"></el-table-column>
         <el-table-column type="index"></el-table-column>
         <el-table-column label="参数名称" prop="attr_name"></el-table-column>
         <el-table-column label="操作">
           <template #default="{row}">
             <el-button @click="showEditDialog(row.attr_id)" size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
             <el-button  @click="removeParams(row.attr_id)" size="mini" type="danger" icon="el-icon-delete">删除</el-button>
           </template>
         </el-table-column>
       </el-table>

        </el-tab-pane>
        <el-tab-pane label="静态属性" name="only">
          <el-button type="primary" size="mini" :disabled="isbtnable" @click="addDialog=true">添加属性</el-button>
          <el-table :data="onlytableData" border stripe>
            <el-table-column type="expand"></el-table-column>
            <el-table-column type="index"></el-table-column>
            <el-table-column label="属性名称" prop="attr_name"></el-table-column>
            <el-table-column label="操作">
              <template #default="{row}">
                <el-button @click="showEditDialog(row.attr_id)" size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
                <el-button  @click="removeParams(row.attr_id)" size="mini" type="danger" icon="el-icon-delete">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>

      </el-tabs>
    </el-card>
    <el-dialog
        @close="addDialogClose"
        :title="titleText"
        :visible.sync="addDialog"
        width="50%"
        >
   <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="100px" >
     <el-form-item :label="titleText" prop="attr_name">
       <el-input v-model="addForm.attr_name"></el-input>
     </el-form-item>
   </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="addDialog = false">取 消</el-button>
    <el-button type="primary" @click="addParams">确 定</el-button>
  </span>
    </el-dialog>
<!--    修改-->
    <el-dialog
        @close="editDialogClose"
        :title="titleText"
        :visible.sync="editDialog"
        width="50%"
    >
      <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="100px" >
        <el-form-item :label="titleText" prop="attr_name">
          <el-input v-model="editForm.attr_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="editDialog = false">取 消</el-button>
    <el-button type="primary" @click="editParams">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>
<script>
import {changebianji, delcategories, getmany, getparamsApi, postaddForm, putchangebianji} from "@/api/user";

export default {
  data() {
    return {
      editFormRules:{attr_name:[{required: true, message: '请输入参数名称', trigger: 'blur'}]},
      editForm:{},
      editDialog:false,
      addForm: {
        attr_name: ''
      },
      addFormRules: {
        attr_name: [{required: true, message: '请输入参数名称', trigger: 'blur'}]
      },
      addDialog: false,
      catelist: [],
      cateprops: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      selecatKeys: [],
      activeName: 'many',
      manytableData: [],
      onlytableData: []

    }
  },
  created() {
    this.getparamsList()
  },
  computed: {
    isbtnable() {
      if (this.selecatKeys.length !== 3) {
        return true
      }
      return false
    },
    cateId() {
      if (this.selecatKeys.length === 3) {
        return this.selecatKeys[2]
      }
      return null
    },
    titleText() {
      if (this.activeName === 'many') {
        return '动态参数'
      }
      return '静态属性'
    }
  },
  methods: {
    async getparamsList() {
      const res = await getparamsApi()
      // console.log(res)
      this.catelist = res.data
    },
    handleChange() {
      this.getparamsData()
    },
    async getparamsData() {
      if (this.selecatKeys.length !== 3) {
        this.selecatKeys = []
        return
      }
      const res = await getmany(this.cateId, {sel: this.activeName})
      // console.log(res)
      if (this.activeName === 'many') {
        this.manytableData = res.data
      } else {
        this.onlytableData = res.data
      }
    },
    handleClick() {
      this.getparamsData()
    },
    addDialogClose() {
      this.$refs.addFormRef.resetFields()
    },
    async addParams() {
      const res = await this.$refs.addFormRef.validate()
//       // console.log(res
      const res1 = await postaddForm(this.cateId, {attr_name: this.addForm.attr_name, attr_sel: this.activeName})
      console.log(res1)
      this.$message.success('添加成功')
   this.getparamsData()
       this.addDialog=false
    },
   async showEditDialog(attr_id) {
const res = await changebianji(this.cateId,attr_id,{attr_sel:this.activeName})
     console.log(res)
     this.editForm=res.data
     this.editDialog=true
    },
    editDialogClose() {
this.$refs.editFormRef.resetFields()
    },
    async editParams() {
 const res =await this.$refs.editFormRef.validate()
      if(res) {
        const res1 =await putchangebianji(this.cateId,this.editForm.attr_id, {attr_name:this.editForm.attr_name,attr_sel:this.activeName})
        console.log(res1)
        this.$message.success('修改成功')
        this.getparamsData()
        this.editDialog=false
      }
    },
   async removeParams(attr_id) {
     console.log(attr_id)
     const confimresult =await this.$confirm('此操作将永久删除该参数, 是否继续?', '提示', {
       confirmButtonText: '确定',
       cancelButtonText: '取消',
       type: 'warning'
     }).catch(err=>err)
     if(confimresult!=='confirm') {
       return this.$message.info('已取消删除')
     }
const res1 = await delcategories(this.cateId,attr_id)

     this.$message.success('删除成功')
     this.getparamsData()
    }

  }
}
</script>
<style lang="less" scoped>
.cat {
  margin: 15px 0;
}

.el-card {
  margin-top: 20px;
}
</style>